package edu.uestc.canary.controller;

import edu.uestc.canary.entity.Category;
import edu.uestc.canary.entity.dto.Result;
import edu.uestc.canary.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * Spring MVC允许跨域
 *  1.通过@CrossOrigin
 *      value/origins: 允许跨域请求的url
 *      allowedHeaders: 允许的请求头
 *      methods: 允许跨域的http请求方法
 *      allowCredentials: 是否允许携带跨域的cookie
 *  2. 通过spring mvc配置允许跨域 addCorsMappings
 *
 *  3.提供过滤器
 */
@RestController
@RequestMapping("/api/categories")

public class CategoryController {
    @Resource private CategoryService service;

    @PostMapping
    public Result save(@RequestBody Category category){
        return service.save(category) ? Result.ok(category) : Result.fail("添加类别信息失败");
    }

    @PutMapping
    public Result update(@RequestBody Category category){
        return service.modify(category) ? Result.ok(category) : Result.fail("更新类别信息失败");
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id){
        return service.removeById(id) ? Result.ok() : Result.fail("删除类别信息失败");
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        return Result.ok(service.findById(id));
    }

    @GetMapping
    public Result loadAll(){
        return Result.ok(service.loadAll());
    }
}
